<!--
  - Copyright (C) 2024
  - All rights reserved, Designed By www.joolun.com
  - 注意：
  - 本软件为www.joolun.com开发研制，未经购买不得使用
  - 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
  - 一经发现盗用、分享等行为，将追究法律责任，后果自负
-->
<template>
	<view class="bg-white">

		<mescroll-body ref="mescrollRef" @down="downCallback" @up="upCallback">
			<view class="bgs">
				<view class="logs">
					<image class="logsimg" mode="widthFix"
						src="https://hqtsc.oss-cn-beijing.aliyuncs.com/1/material/84c02118-4664-445c-8bf1-6688d6ac67e0.png" />
					<view class="names">环球通医药</view>
				</view>
				<view class="turnover padding-sm">
					<view class="storefront">
						<view class="flex justify-between align-center">
							<!-- <view class="flex align-center">
							<view class="cu-avatar round flex head-bg bg-white"
								:style="'background-image:url('+this.userInfo.avatar+')'"></view>
							<view class="margin-left-sm">
								<view class="text-df  text-left" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 90%;">{{shopInfo.name||userInfo.nickName}}</view>
								<view class="text-sm  text-left" v-if="userInfo.type=='2'">{{$t('home.index.h57o16')}}{{shopInfo.collectCount}}</view>
							</view>
						</view> -->
							<navigator class="cu-btn round bg-white text-sm sm guanli" url="shop-home">
								{{$t('home.index.o5y08i')}}
							</navigator>
						</view>
						<view class="pricebox">
							<view class="price">
								<view class="price_flex">
									<view>当日营业额</view>
									<view>{{analyze_data.rev}}元</view>
								</view>
								<view class="price_flex">
									<view>当日订单量</view>
									<view>{{analyze_data.ord_cnt}}</view>
								</view>
							</view>
							<view class="price">
								<view class="price_flex">
									<view>今日新增用户</view>
									<view>{{analyze_data.new_usr}}人</view>
								</view>
								<view class="price_flex">
									<view>今日新增商品</view>
									<view>{{analyze_data.new_spu}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<navigator class="bg-5" url="/package-live/pages/room/list">
				<view class="flex align-center margin-left margin-top-xl">
				</view>
			</navigator>
			<view v-if="userInfo.type!='3'" class="flex justify-center margin-top-xl">
				<navigator class="bg-1" url="../mall/order/list?status=1">
					<view class="flex align-center margin-left margin-top-sm">
						<image src="/static/public/icon/logistics.png" class="icon"></image>
						<view class="text-white text-sm margin-left-xs">{{$t('home.index.28g45r')}}</view>
					</view>
					<view class="text-white text-center quantity margin-top-xs text-bold">{{showCount1}}</view>
				</navigator>
				<navigator class="bg-2 margin-left" url="../mall/order/list?status=0">
					<view class="flex align-center margin-left margin-top-sm">
						<image src="/static/public/icon/payment.png" class="icon"></image>
						<view class="text-white text-sm margin-left-xs">{{$t('home.index.88oddj')}}</view>
					</view>
					<view class="text-white text-center quantity margin-top-xs text-bold">{{showCount2}}</view>
				</navigator>
			</view>
			<view v-if="userInfo.type!='3'" class="flex justify-center margin-top">
				<navigator class="bg-3" url="../mall/order/list?status=2">
					<view class="flex align-center margin-left margin-top-sm">
						<image src="/static/public/icon/the-goods.png" class="icon"></image>
						<view class="text-white text-sm margin-left-xs">{{$t('home.index.7q2j52')}}</view>
					</view>
					<view class="text-white text-center quantity margin-top-xs text-bold">{{showCount3}}</view>
				</navigator>
				<navigator class="bg-4 margin-left" url="../mall/order/list?status=4">
					<view class="flex align-center margin-left margin-top-sm">
						<image src="/static/public/icon/evaluation.png" class="icon"></image>
						<view class="text-white text-sm margin-left-xs">{{$t('home.index.z8dg6b')}}</view>
					</view>
					<view class="text-white text-center quantity margin-top-xs text-bold">{{showCount4}}</view>
				</navigator>
			</view>

			<view class="flex justify-between margin-top-xl padding-left padding-right">
				<view class="text-darkgrey margin-left-sm">{{$t('home.index.731y65')}}</view>
				<navigator class="text-purple-grey margin-right-sm" url="shop-home">{{ $t('home.index.731y61') }}
				</navigator>
			</view>
			<view class="margin margin-top-sm">
				<navigator v-if="userInfo.type!='3'" url="../mall/goodsspu/list">
					<view class="cu-item bg-white flex radius padding-sm module-bg align-center">
						<view class="cu-item s">
							<image class="shop-icon" src="/static/public/icon/icon-8.png"></image>
						</view>
						<view class="flex-sub margin-left-sm">
							<view class="text-darkgrey flex justify-between">
								<view>{{$t('home.index.225ecu')}}</view>
							</view>
							<view class="text-purple-grey margin-top-xs text-sm">{{$t('home.index.6gx7d8')}}</view>
						</view>
						<view class="text-darkgrey text-bold">{{goodsspuCount}}</view>
					</view>
				</navigator>
				<navigator url="../mall/order/list">
					<view class="cu-item bg-white flex radius padding-sm margin-top-sm module-bg align-center">
						<view class="round">
							<image class="shop-icon" src="/static/public/icon/icon-9.png"></image>
						</view>
						<view class="flex-sub margin-left-sm">
							<view class="text-darkgrey flex justify-between">
								<view>{{$t('home.index.7325r4')}}</view>
							</view>
							<view class="text-purple-grey margin-top-xs text-sm">{{$t('home.index.do0n70')}}</view>
						</view>
						<view class="text-darkgrey text-bold">{{orderinfoCount}}</view>
					</view>
				</navigator>
				<!-- <navigator url="../mall/userInfo/list" class="cu-item bg-white flex radius padding-sm margin-top-sm module-bg">
					<view class="round"><image class="shop-icon" src="/static/public/icon/icon-10.png"></image></view>
						<view class="flex-sub margin-left-sm">
							<view class="text-darkgrey flex justify-between">
								<view>用户总量</view>
							</view>
							<view class="text-purple-grey margin-top-xs text-sm">所有商城用户</view>
						</view>
					<view class="margin-right text-darkgrey text-bold">{{userinfoCount}}</view>
				</navigator> -->
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import service from "@/store/service";
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";

	import {
		getCount as getOrderInfoCount,
		getPaymentpriceSum
	} from '@/api/mall/orderinfo'
	import {
		getCount as getUserInfoCount
	} from '@/api/mall/userinfo'
	import {
		getCount as getGoodsSpuCount
	} from '@/api/mall/goodsspu'
	import {
		getObj as getObjByShop
	} from '@/api/mall/shopinfo.js'
	import {
		getHomeStatics,
		getToadyOrderCountApi,
		getToadyGoodAddApi,
		getToadyUsrAddApi
	} from "../../api/home/home";

	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				globalData: getApp().globalData,
				CustomBar: this.CustomBar,
				todayPaymentPriceSum: 0, //营业额
				goodsspuCount: 0,
				userinfoCount: 0,
				orderinfoCount: 0,
				showCount1: 0,
				showCount2: 0,
				showCount3: 0,
				showCount4: 0,
				shopCollectCount: 0, //店铺收藏数
				userInfo: {},
				shopInfo: {},
				analyze_data: {
					rev: 0, // 当前营业额（revenue）
					ord_cnt: 0, // 当日订单量
					new_usr: 0, // 今日新增用户
					new_spu: 0 // 今日新增商品（spu = 标准商品）
				}
			};
		},
		onLoad() {
			this.userInfo = service.getUser();
			// 根据用户类型type显示不同的首页数据
			/** 账号类型：-1、系统管理员；1、租户管理员；2、店铺管理员；3、供应商账号 */
			service.saveLogInfo(this.$t('home.index.oi9gxk'))
		},
		onShow() {
			this.getStatisticsData()
		},
		methods: {
			// 获取当日营业额
			async getStatisticsData() {
				const now = new Date();
				const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
				const endOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59);
				const res = await getHomeStatics({
					beginTime: this.formatDateTime(startOfDay),
					endTime: this.formatDateTime(endOfDay)
				})
				if (res.code === 0) {
					this.analyze_data.rev = res.data.sum
					this.analyze_data.new_usr = res.data.count
				}
				console.log('首页分析: ', res)

				const orderCountRes = await getToadyOrderCountApi({
					beginTime: this.formatDateTime(startOfDay),
					endTime: this.formatDateTime(endOfDay)
				})
				if (orderCountRes.code === 0) {
					this.analyze_data.ord_cnt = orderCountRes.data.countToday || 0
				}
				console.log('首页订单数:', orderCountRes)

				// 当日新增商品
				// try {
				// 	const goodsRes = await getToadyGoodAddApi()
				// 	if (goodsRes.code === 0) {
				// 		this.analyze_data.new_spu = goodsRes.data.countToday || 0
				// 	}
				// 	console.log('新增商品:', goodsRes)
				// } catch (err) {
				// 	console.error('新增商品接口报错:', err)
				// }

				// 当日新增用户
				const userRes = await getToadyUsrAddApi()
				if (userRes.code === 0) {
					this.analyze_data.new_usr = userRes.data.countToday
				}
				console.log('新增用户:', userRes)
			},
			formatDateTime(date) {
				const pad = n => n.toString().padStart(2, '0');
				return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
					`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
			},
			initData() {
				service.saveLogInfo(this.$t('home.index.fg5i85'))
				if (this.globalData.access_token && this.userInfo != null) {
					this.getCount();
					this.getPaymentpriceSum();
				}
				service.saveLogInfo(this.$t('home.index.6jc7d1'))
			},
			getPaymentpriceSum() {
				service.saveLogInfo(this.$t('home.index.41u84m'))
				let query = {
					beginTime: this.$moment().format('YYYY-MM-DD 00:00:00'),
					endTime: this.$moment().add(1, 'days').format('YYYY-MM-DD 00:00:00'),
					isPay: 1
				}
				if (this.userInfo.shopId) {
					query.shopId = this.userInfo.shopId;
				}
				getPaymentpriceSum(query).then(response => {
					if (response.data) {
						this.todayPaymentPriceSum = response.data;
					} else {
						this.todayPaymentPriceSum = 0;
					}
				})
			},
			getCount() {
				service.saveLogInfo(this.$t('home.index.5x480c'))
				// 供应商不能加载商品数据
				if (this.userInfo.type == '3') {
					return;
				}
				getGoodsSpuCount().then(response => {
					if (response.data) {
						this.goodsspuCount = response.data;
					} else {
						this.goodsspuCount = 0;
					}
				})
				// getUserInfoCount().then(response=>{
				// 	if(response.data){
				// 		this.userinfoCount = response.data;
				// 	}
				// })
				getOrderInfoCount().then(response => {
					if (response.data) {
						this.orderinfoCount = response.data;
					} else {
						this.orderinfoCount = 0;
					}
				})
				getOrderInfoCount({
					status: '1'
				}).then(response => {
					if (response.data) {
						this.showCount1 = response.data;
					} else {
						this.showCount1 = 0;
					}
				})
				getOrderInfoCount({
					status: '0'
				}).then(response => {
					if (response.data) {
						this.showCount2 = response.data;
					} else {
						this.showCount2 = 0;
					}
				})
				getOrderInfoCount({
					status: '2'
				}).then(response => {
					if (response.data) {
						this.showCount3 = response.data;
					} else {
						this.showCount3 = 0;
					}
				})
				getOrderInfoCount({
					status: '3',
					appraisesStatus: '0'
				}).then(response => {
					if (response.data) {
						this.showCount4 = response.data;
					} else {
						this.showCount4 = 0;
					}
				})
				service.saveLogInfo(this.$t('home.index.hd389c'))
				if (this.userInfo.shopId) {
					getObjByShop(this.userInfo.shopId).then(response => {
						if (response.data) {
							this.shopInfo = response.data;
						}
					})
				}
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				this.initData();
				mescroll.resetUpScroll();
			},
			/*上拉加载的回调: mescroll携带page的参数, 其中num:当前页 从1开始, size:每页数据条数,默认10 */
			upCallback(mescroll) {
				mescroll.endBySize(1, 1); //设置列表数据
			},
		}
	}
</script>

<style>
	.bgs {
		background: linear-gradient(to top, rgb(125 224 253), #007eff);
		padding: 20rpx 0;
		padding-top: 50rpx;
	}

	.turnover {
		width: 90%;
		height: 300rpx;
		margin: 20rpx auto;
		border-radius: 20rpx;
		position: relative;
		color: black !important;

		background-size: 100% 100%;
		background: white;
	}

	.icon {
		width: 36rpx;
		height: 36rpx;
	}

	.bg-1 {
		width: 43%;
		height: 180rpx;
		border-radius: 20rpx;
		background-image: url(/static/public/icon/1.png), -webkit-linear-gradient(45deg, #ec1355, #ff8088);
		background-size: 100% 100%;
		color: #ffffff;
		box-shadow: 0px 10px 20px #ebbdc9;
	}

	.bg-2 {
		width: 43%;
		height: 180rpx;
		border-radius: 20rpx;
		background-image: url(/static/public/icon/2.png), -webkit-linear-gradient(45deg, #a168fe, #753ff1);
		background-size: 100% 100%;
		color: #ffffff;
		box-shadow: 0px 10px 20px #dfbaec;
	}

	.bg-3 {
		width: 43%;
		height: 180rpx;
		border-radius: 20rpx;
		background-image: url(/static/public/icon/3.png), -webkit-linear-gradient(45deg, #634fe8, #69c5ff);
		background-size: 100% 100%;
		color: #ffffff;
		box-shadow: 0px 10px 20px #babbec;
	}

	.bg-4 {
		width: 43%;
		height: 180rpx;
		border-radius: 20rpx;
		background-image: url(/static/public/icon/4.png), -webkit-linear-gradient(45deg, #ffce1d, #f8a74f);
		background-size: 100% 100%;
		color: #ffffff;
		box-shadow: 0px 10px 20px #ebddbf;
	}

	.bg-5 {
		width: 90%;
		height: 200rpx;
		border-radius: 20rpx;
		margin: 20rpx auto;
		background-image: url(https://minio.joolun.com/joolun/1/material/c8bca89d-c30b-40dc-9cca-ca1ba79d7b5a.jpg);
		background-repeat: no-repeat;
		background-size: 100% 100%;
		color: #ffffff;
		box-shadow: 0px 10px 20px #d8dde2;
	}

	.quantity {
		font-size: 68rpx;
	}

	.module-bg {
		height: 120rpx;
		/* box-shadow: 0px 10px 20px #e8e8f3; */
		border-bottom: #f9f9f9 1px solid;
	}

	.shop-icon {
		width: 48rpx;
		height: 48rpx;
		opacity: 0.8;
	}

	.logs {
		height: 100rpx;
		margin: 0rpx 50rpx;
	}

	.logsimg {
		width: 90rpx;
		float: left;
	}

	.names {
		font-size: 40rpx;
		margin-left: 30rpx;
		line-height: 90rpx;
		float: left;
		color: white;
	}

	.storefront {
		position: relative;
	}

	.guanli {
		position: absolute;
		right: -15rpx;
		top: -15rpx;
		font-size: 30rpx !important;
	}

	.pricebox {
		position: relative;
	}

	.price {
		width: 100%;
		display: flex;
	}

	.price:nth-child(1) {
		border-bottom: solid 1px rgb(211, 211, 211);
		margin: 20rpx 0;
		padding-bottom: 10rpx;
	}

	.price_flex {
		flex: 1;
		text-align: center;
	}

	.price_flex>view:nth-child(1) {
		font-size: 35rpx;
		color: #333;
	}

	.price_flex>view:nth-child(2) {
		font-size: 25rpx;
		color: #999;
		margin-top: 20rpx;
	}

	.pricebox::after {
		content: "";
		width: 1px;
		height: 100%;
		background: rgb(211, 211, 211);
		position: absolute;
		left: 50%;
		top: 0px;
		transform: translateX(-50%);
	}
</style>